package com.tanhua.admin.service;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.admin.exception.BusinessException;
import com.tanhua.commons.utils.Constants;
import com.tanhua.dubbo.api.MovementApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.VideoApi;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.mongo.Video;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.MovementsVo;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.VideoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ManageService {

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private VideoApi videoApi;

    @DubboReference
    private MovementApi movementApi;

    @Autowired
    private StringRedisTemplate redisTemplate;


    //查询用户列表
    public PageResult users(Integer page, Integer pagesize) {
        Page<UserInfo> pages = userInfoApi.pages(page, pagesize);
        //查询用户冻结状态并赋值
        List<UserInfo> userInfoList = pages.getRecords();
        for (UserInfo userInfo : userInfoList) {
            if (redisTemplate.hasKey(Constants.USER_FREEZE+userInfo.getId())){
                userInfo.setUserStatus("2");
            }
        }
        PageResult pr = new PageResult(page, pagesize, (int) pages.getTotal(), pages.getRecords());
        return pr;
    }

    //查询用户详情
    public UserInfo userInfo(Long userId) {
        UserInfo userInfo = userInfoApi.findById(userId);
        if (redisTemplate.hasKey(Constants.USER_FREEZE+userInfo.getId())){
            userInfo.setUserStatus("2");
        }
        return userInfo;
    }

    //查看视频列表
    public PageResult videos(Integer page, Integer pagesize, Long uid) {
        //查询视屏列表
        PageResult pr = videoApi.pageByUserId(page, pagesize, uid);
        //判断是否为空
        if (CollUtil.isEmpty(pr.getItems())) {
            return pr;
        }

        List<Video> videoList = pr.getItems();
        //查询用户详情
        UserInfo userInfo = userInfoApi.findById(uid);
        //封装vo
        List<VideoVo> vos = new ArrayList<>();
        for (Video video : videoList) {
            VideoVo vo = VideoVo.init(userInfo, video);
            vos.add(vo);
        }
        //返回结果
        pr.setItems(vos);
        return pr;
    }

    //查看动态列表
    public PageResult messages(Integer page, Integer pagesize, Long uid, Integer state) {
        //查询动态列表
        PageResult pr = movementApi.findPage(page, pagesize, uid, state);
        //判断是否为空
        if (CollUtil.isEmpty(pr.getItems())) {
            return pr;
        }

        List<Movement> list = pr.getItems();
        //查询用户详情
        UserInfo userInfo = userInfoApi.findById(uid);
        //封装vo
        List<MovementsVo> vos = new ArrayList<>();
        for (Movement movement : list) {
            MovementsVo vo = MovementsVo.init(userInfo, movement);
            vos.add(vo);
        }
        //返回结果
        pr.setItems(vos);
        return pr;
    }

    //冻结用户
    public String freeze(Map map) {
        String userId =map.get("userId").toString();
        String freezingTime =map.get("freezingTime").toString();
        Integer freezeDay = 0;
        if ("1".equals(freezingTime)) {
            freezeDay = 3;
        } else if ("2".equals(freezingTime)) {
            freezeDay = 7;
        } else if ("3".equals(freezingTime)){
            freezeDay = -1;
        }
        String json = JSON.toJSONString(map);
        redisTemplate.opsForValue().set(Constants.USER_FREEZE+userId,json, Duration.ofDays(freezeDay));
        return "用户冻结成功";
    }

    //解冻用户
    public Map unfreeze(Map map) {
        long userId = Long.parseLong(map.get("userId").toString());
        redisTemplate.delete(Constants.USER_FREEZE+userId);
        Map retMap = new HashMap();
        retMap.put("message","用户解冻成功");
        return retMap;
    }
}
